name: CD - Deploy image to DockerHub

on:
  workflow_dispatch:
  push:
    branches: [ "main" ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: 'maven'

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}

      - uses: docker/setup-buildx-action@v3

      - name: Set build number
        id: build-number
        run: echo "BUILD_NUMBER=$(date '+%d.%m.%Y.%H.%M.%S')" >> $GITHUB_OUTPUT

      - uses: mr-smithers-excellent/docker-build-push@v5
        with:
          image: ${{ secrets.DOCKERHUB_USERNAME }}/tasklist
          tags: ${{ steps.build-number.outputs.BUILD_NUMBER }}, latest
          dockerfile: Dockerfile
          registry: docker.io
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}

      - name: Send Telegram Message
        run: >
          curl -X POST -H 'Content-type: application/json'
          --data '{"chat_id": "${{ secrets.TELEGRAM_CHAT_ID }}", 
          "text":"Image tag:${{ steps.build-number.outputs.BUILD_NUMBER }} successfuly pushed 
          to https://hub.docker.com/repository/docker/${{ secrets.DOCKERHUB_USERNAME }}/tasklist"}'
          https://api.telegram.org/bot${{ secrets.TELEGRAM_BOT_TOKEN }}/sendMessage
